package Test;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;



public class JListDemo extends JFrame
{

	

		private JPanel contentPane;
		private JList<Integer> list;
		private JButton btnMehr;
		
		private MyListModel model = new MyListModel();

		/**
		 * Launch the application.
		 */
		public static void main(String[] args)
		{
			EventQueue.invokeLater(new Runnable() {
				public void run()
				{
					try
					{
						JListDemo frame = new JListDemo();
						frame.setVisible(true);
					} catch (Exception e)
					{
						e.printStackTrace();
					}
				}
			});
		}

		/**
		 * Create the frame.
		 */
		public JListDemo()
		{
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setBounds(100, 100, 174, 300);
			contentPane = new JPanel();
			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
			contentPane.setLayout(new BorderLayout(0, 0));
			setContentPane(contentPane);
			
			JScrollPane scrollPane = new JScrollPane();
			contentPane.add(scrollPane, BorderLayout.CENTER);
			
			list = new JList<Integer>(model);
			list.setCellRenderer(new MyRenderer());
			scrollPane.setViewportView(list);
			
			btnMehr = new JButton("mehr");
			btnMehr.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					do_btnMehr_actionPerformed(arg0);
				}
			});
			contentPane.add(btnMehr, BorderLayout.SOUTH);
		}
		
		
		
		
		private class MyListModel implements ListModel<Integer>
		{
			private ArrayList<Integer> liste = new ArrayList<>();
			private List<ListDataListener> listeners = new ArrayList<>();

			public MyListModel()
			{
				liste.add(1);
				liste.add(2);
				liste.add(5);
			}

			public void mehr()
			{
				liste.add(5);
				for (ListDataListener l : listeners)
					l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, liste.size()-1, liste.size()-1));
			}


			@Override
			public void addListDataListener(ListDataListener l)
			{
				listeners.add(l);
				
			}

			@Override
			public Integer getElementAt(int index)
			{
				return liste.get(index);
			}

			@Override
			public int getSize()
			{
				return liste.size();
			}

			@Override
			public void removeListDataListener(ListDataListener l)
			{
				listeners.remove(l);
				
			}
			
		}
		
		
		private class MyRenderer implements ListCellRenderer<Integer>
		{

			@Override
			public Component getListCellRendererComponent(
					JList<? extends Integer> liste, Integer value, int index,
					boolean selected, boolean focus)
			{
				JLabel label = new JLabel("Wert: " + value);
				label.setOpaque(true);
				if (selected)
				{
					label.setBackground(liste.getSelectionBackground());
					label.setForeground(liste.getSelectionForeground());
				}
				else
				{
					label.setBackground(liste.getBackground());
					label.setForeground(liste.getForeground());
				}
				
				return label;
			}
			
		}
		
		

		protected void do_btnMehr_actionPerformed(ActionEvent arg0) 
		{
			model.mehr();
			list.validate();
			list.repaint();
		}
}
